package com.corner.ui.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.corner.R;
import com.corner.app.ConsTants;
import com.corner.presenter.imp.UserSettingItemPresenter;
import com.corner.ui.activity.SetPasswordActivity;
import com.corner.ui.activity.UserSettingActivity;
import com.corner.ui.base.BaseActivity;
import com.corner.ui.base.BaseMVPAdapter;
import com.corner.ui.view.IUserSettingItemView;
import com.corner.utils.DialogUtil;
import com.corner.utils.ToastUtil;
import com.corner.utils.TypeFaceUtil;
import com.corner.utils.view.CustomFont;
import com.corner.utils.view.FindView;
import com.corner.utils.view.InjectViewUtils;

/**
 * Created by Administrator on 2015/5/23.
 */
public class UserSettingAdapter extends BaseMVPAdapter<UserSettingItemPresenter> implements View.OnClickListener, IUserSettingItemView {

    private static final int LOGOUT = 1;
    private static final int CHANGE_PASSWORD = 2;
    Dialog changeUserName;
    Dialog setPassword;
    private Context context;
    private String[] itemStrs;
    private TextView tvChangeEnsure;
    private TextView tvChangeCancel;
    private EditText etNewUsername;
    private TextView tvSetEnsure;
    private TextView tvSetCancel;
    private TextView tvSetTitle;
    private String nickName;
    private Dialog isLogoutDialog;

    public UserSettingAdapter(Context context) {
        this.context = context;
        itemStrs = context.getResources().getStringArray(R.array.user_setting);
    }

    @Override
    public int getCount() {
        return itemStrs.length;
    }

    @Override
    public Object getItem(int position) {
        return itemStrs[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.lv_user_setting_item, null);
            Holder holder = new Holder();
            InjectViewUtils.injectView(holder, convertView, context);
            convertView.setTag(holder);
        }
        Holder holder = (Holder) convertView.getTag();
        holder.llItem.setOnClickListener(this);
        holder.llItem.setTag(R.integer.user_setting_item_position, position);
        holder.tvName.setText(itemStrs[position]);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        if (v.getTag(R.integer.user_setting_item_position) != null) {
            int position = (int) v.getTag(R.integer.user_setting_item_position);
            switch (v.getId()) {
                case R.id.ll_user_setting_item:
                    switch (position) {
                        case 0:
                            showChangeUsernameDialog();
                            break;
                        case 1:
                            changePassword();
                            break;
                        case 2:
                            logout();
                            break;
                    }
                    break;
            }
        } else {
            switch (v.getId()) {
                case R.id.tv_dialog_change_username_ensure:
                    if (TextUtils.isEmpty(etNewUsername.getText())) {
                        ToastUtil.showCustomToastInRelease(context, context.getString(R.string.change_username_empty_notice));
                    } else {
                        changeUsername();
                        DialogUtil.hideDialog(changeUserName);
                    }
                    break;
                case R.id.tv_dialog_change_username_cancel:
                    DialogUtil.hideDialog(changeUserName);
                    break;
                case R.id.tv_user_setting_set_password_ensure:
                    Intent intent = new Intent(context, SetPasswordActivity.class);
                    context.startActivity(intent);
                    DialogUtil.hideDialog(setPassword);
                    break;
                case R.id.tv_user_setting_set_password_cancel:
                    DialogUtil.hideDialog(setPassword);
                    break;
                case R.id.tv_logout_ensure:
                    DialogUtil.hideDialog(isLogoutDialog);
                    presenter.logout(context);
                    break;
                case R.id.tv_logout_cancel:
                    DialogUtil.hideDialog(isLogoutDialog);
                    break;
            }
        }
    }

    private void logout() {

        presenter.getIsHasPasswordBeforeLogout(context);
    }

    private void changePassword() {
        ((BaseActivity) context).excuteTaskWhenNetOk(new BaseActivity.Task() {
            @Override
            public void onExcute() {
                presenter.changePassword(context);
            }
        });
    }

    /**
     * 修改用户名
     */
    private void changeUsername() {
        ((BaseActivity) context).excuteTaskWhenNetOk(new BaseActivity.Task() {
            @Override
            public void onExcute() {
                presenter.changeUsername(etNewUsername.getText().toString());
            }
        });
    }

    /**
     * 修改用户名
     */
    private void showChangeUsernameDialog() {
        if (changeUserName == null) {
            changeUserName = DialogUtil.showDiaLog(R.layout.dialog_change_nickname, context);
            TextView tvTitle = (TextView) changeUserName.findViewById(R.id.tv_dialog_change_username_title);
            etNewUsername = (EditText) changeUserName.findViewById(R.id.et_dialog_change_username_new_username);
            tvChangeEnsure = (TextView) changeUserName.findViewById(R.id.tv_dialog_change_username_ensure);
            TypeFaceUtil.setTypeFace(context, tvTitle, ConsTants.TYPE_FACE);
            TypeFaceUtil.setTypeFace(context, etNewUsername, ConsTants.TYPE_FACE);
            TypeFaceUtil.setTypeFace(context, tvChangeEnsure, ConsTants.TYPE_FACE);
            tvChangeEnsure.setOnClickListener(this);

            tvChangeCancel = (TextView) changeUserName.findViewById(R.id.tv_dialog_change_username_cancel);
            TypeFaceUtil.setTypeFace(context, tvChangeCancel, ConsTants.TYPE_FACE);
            tvChangeCancel.setOnClickListener(this);
        } else {
            changeUserName.show();
        }
        etNewUsername.setHint(nickName);
        etNewUsername.setText(null);
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @Override
    protected UserSettingItemPresenter createP() {
        return new UserSettingItemPresenter();
    }

    @Override
    public void changeUsernameSucess() {
        ToastUtil.showCustomToastInRelease(context, context.getString(R.string.change_username_sucess));
        ((UserSettingActivity) context).refresh();
    }

    @Override
    public void changeUsernameError(String message) {
        ToastUtil.showCustomToastInRelease(context, message);
    }

    @Override
    public void logoutError(String message) {
        ToastUtil.showCustomToastInRelease(context, message);
    }

    @Override
    public void showLogoutHasNoPasswordDialog() {
        showHasNoPasswordDialog(LOGOUT);

    }

    @Override
    public void changePasswordError(String message) {
        ToastUtil.showCustomToastInRelease(context, message);
    }

    @Override
    public void showChangePasswordHasNoPasswordDialog() {
        showHasNoPasswordDialog(CHANGE_PASSWORD);
    }

    @Override
    public void showLogoutEnsureDialog() {
        if (isLogoutDialog == null) {
            isLogoutDialog = DialogUtil.showDiaLog(R.layout.dialog_logout_is_ok, context);
            TextView tvLogoutEnsure = (TextView) isLogoutDialog.findViewById(R.id.tv_logout_ensure);
            TextView tvLogoutCancel = (TextView) isLogoutDialog.findViewById(R.id.tv_logout_cancel);
            tvLogoutEnsure.setOnClickListener(this);
            tvLogoutCancel.setOnClickListener(this);
        } else {
            isLogoutDialog.show();
        }
    }

    private void showHasNoPasswordDialog(int titleFlag) {
        if (setPassword == null) {
            setPassword = DialogUtil.showDiaLog(R.layout.dialog_user_setting_set_password, context);
            tvSetTitle = (TextView) setPassword.findViewById(R.id.tv_user_setting_set_password_title);
            tvSetEnsure = (TextView) setPassword.findViewById(R.id.tv_user_setting_set_password_ensure);
            TypeFaceUtil.setTypeFace(context, tvSetTitle, ConsTants.TYPE_FACE);
            TypeFaceUtil.setTypeFace(context, tvSetEnsure, ConsTants.TYPE_FACE);

            tvSetEnsure.setOnClickListener(this);

            tvSetCancel = (TextView) setPassword.findViewById(R.id.tv_user_setting_set_password_cancel);
            TypeFaceUtil.setTypeFace(context, tvSetCancel, ConsTants.TYPE_FACE);
            tvSetCancel.setOnClickListener(this);
        } else {
            setPassword.show();
        }
        switch (titleFlag) {
            case LOGOUT:
                tvSetTitle.setText(context.getString(R.string.user_setting_logout_has_not_set_password));
                break;
            case CHANGE_PASSWORD:
                tvSetTitle.setText(R.string.user_setting_has_not_set_passowrd);
                break;
        }
    }

    @Override
    public void showLoading(boolean isShow) {
        ((BaseActivity) context).showLoading(isShow);
    }

    class Holder {
        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_user_setting_item_name)
        private TextView tvName;
        @FindView(R.id.ll_user_setting_item)
        private LinearLayout llItem;
    }
}
